import firebrick
import rwb
import wx
import wx.lib.agw.aui as aui
import wx.lib.agw.customtreectrl as customtreectrl
import wx.lib.mixins.listctrl as listmix
from rwb.process import Process, EVT_PROCESS_READABLE, EVT_PROCESS_ENDED


FID = "rwb.robot.runner"

class RobotRunnerPlugin(firebrick.Plugin):
    '''A plugin for running robot tests'''

    def __init__(self, app):
        super(RobotRunnerPlugin,self).__init__(_id=FID,
                                               url=None, version="0.1", 
                                               name="Robot Test Runner Plugin",
                                               default_settings = {})
    def enable(self):
        self.active = True
        rwb.app.Register(RobotRunnerLogPanel)

    def disable(self):
        self.active = False
        rwb.app.Unregister(RobotRunnerLogPanel)


class CustomListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, *args, **kwargs):
        super(CustomListCtrl, self).__init__(*args, **kwargs)
        # is the following necessary?
        listmix.ListCtrlAutoWidthMixin.__init__(self)
#        self.Bind(wx.EVT_MOTION, self.OnMouseMotion)

class RobotRunnerLogPanel(firebrick.BasePanel):
    name = "Robot Log"
    _id = "rwb.robot.log"

    # if defined, the panel will appear as a child of
    # the given panel
    container = "rwb.core.shelf_notebook"

    def __init__(self, parent):
        super(RobotRunnerLogPanel, self).__init__(parent)
        self.window = parent
        self.listctrl = CustomListCtrl(self, wx.ID_ANY, 
                                       style=wx.LC_REPORT)
        self.process = None
        self.listctrl.InsertColumn(0, "name")
        self.listctrl.InsertColumn(1, "status")

#        self.listctrl.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.listctrl, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.window.Bind(EVT_PROCESS_READABLE, self.OnProcessReadable)
        self.window.Bind(EVT_PROCESS_ENDED, self.OnProcessEnded)

    def OnProcessReadable(self, event):
        print "readable..."
        (stdout, stderr) = self.process.read()
        if stdout:
            print "stdout:", stdout
        if stderr:
            print "stderr:", stderr

    def OnProcessEnded(self, event):
        self.process = None

    def OnItemActivated(self, event):
        item = event.GetItem()
        violation = self.violations[item.GetData()]
        editor = self.window.GetPanel("rwb.core.edit_notebook")
        editor.Open(violation.test, target=violation.line)
        event.Skip()

    def Run(self, suite):
        self.window.ShowPanels("rwb.robot.log")
        self.window.auimgr.Update()
        self.listctrl.DeleteAllItems()
        settings = wx.GetApp().GetSettings(FID)
        cmd = (settings["robot_command"], suite.source)
#        cmd = ["st","rsa","/home/boakley/dev/ci/orbitz-web-wl-ci/servicetest/src/robot"]
        print "running", " ".join(cmd)
#        self.process = Process(cmd, target=self.window)
